//
//  CWShortVideoCollectionCell.swift
//  SwiftTest
//
//  Created by 陈旺 on 2022/2/8.
//

import UIKit

class CWShortVideoCollectionCell: UICollectionViewCell {
    
    //MARK: - 属性
    private(set) var playerView: CWPlayerView
    
    /// 播放视图右边距
    private var playViewRightMargin: NSLayoutConstraint!
    
    /// 播放视图下边距
    private var playViewBottomMargin: NSLayoutConstraint!
    
    /// 按钮列表视图
    private lazy var rightButtons: ActionButtonsView = {
        let target = ActionButtonsView.init(frame: CGRect.zero)
        return target
    }()
    
    /// 下标
    private(set) lazy var titleLab: UILabel = {
        return UILabel.init(frame: CGRect.zero)
            .link
            .textColor(.white)
            .font(.systemFont(ofSize: 20, weight: .medium))
            .end
    }()
    
    //MARK: - 重写
    override init(frame: CGRect) {
        playerView = CWPlayerView.init(frame: CGRect.zero)
        super.init(frame: frame)
        configureCell()
    }
    
    required init?(coder: NSCoder) {
        playerView = CWPlayerView.init(frame: CGRect.zero)
        super.init(coder: coder)
    }
    
    //MARK: - UI
    private func configureCell() {
        let map: [String: UIView] = [
            "player": playerView,
            "lab": titleLab,
            "buttons": rightButtons
        ]
        map.addTo(contentView)
        contentView.withVFL("H:|[player]", views: map)
        contentView.withVFL("V:|[player]", views: map)
        contentView.withVFL("H:|-20-[lab]", views: map)
        contentView.withVFL("V:|-100-[lab]", views: map)
        contentView.withVFL("V:[buttons]-200-|", views: map)
        
        playViewRightMargin = playerView.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: 0)
        playViewRightMargin.isActive = true
        playViewBottomMargin = playerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0)
        playViewBottomMargin.isActive = true
        rightButtons.rightAnchor.constraint(equalTo: playerView.rightAnchor).isActive = true
        
        contentView.bringSubviewToFront(rightButtons)
        contentView.bringSubviewToFront(titleLab)
    }
    
    func move(_ percent: CGFloat, total: CGFloat) {
        let rightMargin = total * percent
        playViewRightMargin.constant = rightMargin * -1
        
        rightButtons.transform = CGAffineTransform.init(translationX: 0, y: -100 * percent)
        rightButtons.alpha = 1 - percent
        
        layoutIfNeeded()
    }
}
